import moment from 'moment';

export const header = 'picture';

/**
 * 获取token
 * @return {string/Boolean} 返回token，若无会返回false
 */
export function getToken() {
  const token = sessionStorage.getItem(`${header}-token`);
  const expired = sessionStorage.getItem(`${header}-expired`);

  if (token && expired && moment(expired) > moment()) {
    return token;
  } else {
    deleteToken();

    return false;
  }
}

/**
 * 设置token
 * @param {String} token 鉴权token
 * @param {String} expired 过期时间
 */
export function setToken(token, expired) {
  sessionStorage.setItem(`${header}-token`, token);
  sessionStorage.setItem(`${header}-expired`, expired);
}

/**
 * 删除token
 */
export function deleteToken() {
  sessionStorage.removeItem(`${header}-token`);
  sessionStorage.removeItem(`${header}-expired`);
  localStorage.removeItem(`${header}-authority`);
  localStorage.removeItem('tempImg');
  localStorage.removeItem('brife');
  sessionStorage.removeItem(`${header}-uid`);
}

/**
 * 获取Uid
 */
export function getUid() {
  return sessionStorage.getItem(`${header}-uid`);
}

export function setUid(uid) {
  sessionStorage.setItem(`${header}-uid`, uid);
}
